//
//  Created by apple on 15/8/28.
//  Copyright (c) 2015年 lyq. All rights reserved.
//

#import "AlertView.h"

@implementation AlertView

-(instancetype)initWithTitle:(NSString *)_title textFieldText:(NSString *)_text type:(AlertType)type{

    self = [super init];
    self.alertType = type;
    self.mainTitle = _title;
    self.subTitle = _text;
    self.frame = CGRectMake(0, 0,DEVW,DEVH);
    self.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.4];
    [self showSubViews];
    return self;
}

- (void) showSubViews {
    _mainAlertView = [[UIScrollView alloc]initWithFrame:CGRectMake(MAIN_ALERT_MARGIN_LEFT,(DEVH-MAIN_ALERT_HEIGHT)/2.0,DEVW-MAIN_ALERT_MARGIN_LEFT*2,MAIN_ALERT_HEIGHT)];
    _mainAlertView.backgroundColor = [UIColor whiteColor];
    _mainAlertView.layer.cornerRadius = 6.0f;
    //裁剪子视图
    _mainAlertView.clipsToBounds = YES;
    _mainAlertView.autoresizingMask = UIViewAutoresizingFlexibleBottomMargin | UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleTopMargin;
    [self addSubview:_mainAlertView];
    
    lab_title = [[UILabel alloc]initWithFrame:CGRectMake(0, 5,_mainAlertView.frame.size.width, 40)];
    lab_title.text = self.mainTitle;
    lab_title.textAlignment = NSTextAlignmentCenter;
    lab_title.textColor = GET_HEX_COLOR(TEXT_DEEP_BLACK);
    [_mainAlertView addSubview:lab_title];
    
    UIView *view_line = UIView.new;
    [_mainAlertView addSubview:view_line];
    view_line.backgroundColor = GET_HEX_COLOR(TABLE_SEPARATOR_LIGHTGRAY);
    view_line.sd_layout.topSpaceToView(lab_title,0).leftSpaceToView(_mainAlertView,0).rightSpaceToView(_mainAlertView,0).heightIs(1);
    
    lab_content = [[UILabel alloc]initWithFrame:CGRectMake(CELL_LEFT_MARGIN, CGRectGetMaxY(lab_title.frame) + 10,_mainAlertView.frame.size.width - CELL_LEFT_MARGIN*2, 60)];
    lab_content.text = self.subTitle;
    lab_content.numberOfLines = 3;
    lab_content.font = GET_SYSTEM_FONT(TEXT_FONT_32);
    lab_content.textAlignment = NSTextAlignmentCenter;
    lab_content.textColor = GET_HEX_COLOR(TEXT_GRAY);
    [_mainAlertView addSubview:lab_content];

    _comfirBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    _comfirBtn.frame = CGRectMake(0,_mainAlertView.frame.size.height-44,_mainAlertView.frame.size.width/2.0 + 1, 44);
    _comfirBtn.tag = 0;
    _comfirBtn.layer.borderWidth = 1;
    _comfirBtn.layer.borderColor = GET_HEX_COLOR(TABLE_SEPARATOR_LIGHTGRAY).CGColor;
    [_comfirBtn setTitle:@"取消" forState:UIControlStateNormal];
    [_comfirBtn setTitleColor:GET_HEX_COLOR(TEXT_GRAY) forState:UIControlStateNormal];
    [_mainAlertView addSubview:_comfirBtn];
    [_comfirBtn addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
    
    _cancleBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    _cancleBtn.frame = CGRectMake(_mainAlertView.frame.size.width/2.0,_mainAlertView.frame.size.height-44,_mainAlertView.frame.size.width/2.0, 44);
    _cancleBtn.tag = 1;
    _cancleBtn.layer.borderWidth = 1;
    [_cancleBtn setTitle:@"前往" forState:UIControlStateNormal];
    _cancleBtn.layer.borderColor = GET_HEX_COLOR(TABLE_SEPARATOR_LIGHTGRAY).CGColor;
    [_cancleBtn setTitleColor:GET_HEX_COLOR(CELL_LABLE_BORDER_RED) forState:UIControlStateNormal];
    [_mainAlertView addSubview:_cancleBtn];
    [_cancleBtn addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
}

//弹出动画
-(void)exChangeOut:(UIView *)changeOutView dur:(CFTimeInterval)dur{
    CAKeyframeAnimation * animation;
    animation = [CAKeyframeAnimation animationWithKeyPath:@"transform"];
    animation.duration = dur;
    animation.removedOnCompletion = NO;
    animation.fillMode = kCAFillModeForwards;
    NSMutableArray *values = [NSMutableArray array];
    [values addObject:[NSValue valueWithCATransform3D:CATransform3DMakeScale(0.1, 0.1, 1.0)]];
    [values addObject:[NSValue valueWithCATransform3D:CATransform3DMakeScale(1.0, 1.0, 1.0)]];
    animation.values = values;
    animation.timingFunction = [CAMediaTimingFunction functionWithName: @"easeInEaseOut"];
    [changeOutView.layer addAnimation:animation forKey:nil];
}

-(void)layoutSubviews{
    [super layoutSubviews];
    DeLog(@"layoutSubviews");
}

-(void)buttonClicked:(UIButton *)button{
    if (self.alertViewDelegate) {
        [self.alertViewDelegate alertViewDelegate:self clickedButtonAtIndex:button.tag];
    }
    [self dismis];
}

-(void)show{
    UIWindow *window = [UIApplication sharedApplication].keyWindow;
    [window addSubview:self];
    [self exChangeOut:_mainAlertView dur:0.2];
}

-(void)dismis{
    [self endEditing:YES];
    [self removeFromSuperview];
}

@end
